﻿Imports OMANE.Common.UI
Imports System.Windows.Controls.Primitives
Imports System.Windows.Controls

Public Class ViewDataStructure

    Private Sub ThumbDragDelta(ByVal sender As System.Object, ByVal e As System.Windows.Controls.Primitives.DragDeltaEventArgs)
        Dim th As Thumb = sender

        Dim tbl = TryCast(th.DataContext, DataTable)

        Dim parent As ListBoxItem = th.TemplatedParent

        If Not tbl.ExtendedProperties.ContainsKey("CanvasLeft") Then
            tbl.ExtendedProperties.Add("CanvasLeft", 0)
        End If
        If Not tbl.ExtendedProperties.ContainsKey("CanvasTop") Then
            tbl.ExtendedProperties.Add("CanvasTop", 0)
        End If

        tbl.ExtendedProperties("CanvasLeft") = CDbl(tbl.ExtendedProperties("CanvasLeft")) + e.HorizontalChange
        tbl.ExtendedProperties("CanvasTop") = CDbl(tbl.ExtendedProperties("CanvasTop")) + e.VerticalChange

        'Dim oleft = If(Double.IsNaN(Canvas.GetLeft(parent)), 0, Canvas.GetLeft(parent))
        'Dim otop = If(Double.IsNaN(Canvas.GetTop(parent)), 0, Canvas.GetTop(parent))

        'Canvas.SetLeft(parent, oleft + e.HorizontalChange)
        'Canvas.SetTop(parent, otop + e.VerticalChange)

        Canvas.SetLeft(parent, CDbl(tbl.ExtendedProperties("CanvasLeft")))
        Canvas.SetTop(parent, CDbl(tbl.ExtendedProperties("CanvasTop")))
    End Sub


    Private Sub Button_Click_1(sender As Object, e As Windows.RoutedEventArgs)
        Dim temp = New ViewRelations()
        CType(temp.RelationsDataGrid.Columns(1), DataGridComboBoxColumn).ItemsSource = ModuleDashboarding._activeproject.DataSet.Tables.Cast(Of DataTable).Select(Function(t) t.TableName)
        CType(temp.RelationsDataGrid.Columns(2), DataGridComboBoxColumn).ItemsSource = ModuleDashboarding._activeproject.DataSet.Tables.Cast(Of DataTable).Select(Function(t) t.TableName)
        _DetailsDataGrid = temp.RelationDetailsDataGrid
        temp.DataContext = New ObjectModel.ObservableCollection(Of DataRelationWrapper)( _
            ModuleDashboarding._activeproject.DataSet.Relations.Cast(Of DataRelation).Select(Function(r) New DataRelationWrapper(r)))


        ModuleDashboarding._uiservice.ShowMessage(temp, Width:=400, Height:=400)

        Dim o = 1

        For Each rel As DataRelationWrapper In CType(temp.DataContext, IEnumerable)

            If rel.DataRelation Is Nothing Then
                Try
                    ModuleDashboarding._activeproject.DataSet.Relations.Add( _
                                       rel.RelationName, _
                                      (From c In rel.Columns Select ModuleDashboarding._activeproject.DataSet.Tables(rel.ParentTable).Columns(c.ParentDataColumn)).ToArray(), _
                                      (From c In rel.Columns Select ModuleDashboarding._activeproject.DataSet.Tables(rel.ChildTable).Columns(c.ChildDataColumn)).ToArray())
                Catch ex As Exception
                    ModuleDashboarding._uiservice.ShowMessage("Imossible de creer la relation " & rel.RelationName & vbCrLf & ex.Message)
                End Try

            End If
        Next

    End Sub

    Private Shared _DetailsDataGrid As DataGrid

    Public Class DataRelationWrapper
        Inherits UIObject



        Sub New(r As DataRelation)
            ' TODO: Complete member initialization 
            _DataRelation = r
            ParentTable = r.ParentTable.TableName
            ChildTable = r.ChildTable.TableName
        End Sub

        Sub New()

        End Sub

        Private _DataRelation As DataRelation
        Public ReadOnly Property DataRelation As DataRelation
            Get
                Return _DataRelation
            End Get
        End Property

        Public ReadOnly Property IsEnabled As Boolean
            Get
                Return _DataRelation Is Nothing
            End Get
        End Property

        Private _Columns As IList(Of DataRelationColumnWrapper)
        Public ReadOnly Property Columns As IList(Of DataRelationColumnWrapper)
            Get
                If _Columns Is Nothing Then
                    If _DataRelation IsNot Nothing Then
                        _Columns = New ObjectModel.ReadOnlyObservableCollection(Of DataRelationColumnWrapper)(New ObjectModel.ObservableCollection(Of DataRelationColumnWrapper)( _
                            From i In Enumerable.Range(0, _DataRelation.ChildColumns.Count()) Select New DataRelationColumnWrapper(Me, i)))
                    Else
                        _Columns = New ObjectModel.ObservableCollection(Of DataRelationColumnWrapper)()
                    End If
                End If
                Return _Columns
            End Get
        End Property

        Private _RelationName As String
        Public Property RelationName As String
            Get
                If _DataRelation IsNot Nothing Then
                    Return _DataRelation.RelationName
                End If
                Return _RelationName
            End Get
            Set(ByVal value As String)
                _RelationName = value
                OnPropertyChanged("RelationName")
            End Set
        End Property

        Private _ParentTable As String
        Public Property ParentTable As String
            Get
                If _DataRelation IsNot Nothing Then
                    Return _DataRelation.ParentTable.TableName
                End If
                Return _ParentTable
            End Get
            Set(ByVal value As String)
                _ParentTable = value
                OnPropertyChanged("ParentTable")
                If _ParentTable Is Nothing Then
                    CType(_DetailsDataGrid.Columns(0), DataGridComboBoxColumn).ItemsSource = Nothing
                Else
                    CType(_DetailsDataGrid.Columns(0), DataGridComboBoxColumn).ItemsSource = _
                        ModuleDashboarding._activeproject.DataSet.Tables(_ParentTable).Columns.Cast(Of DataColumn).Select(Function(c) c.ColumnName).ToList()
                End If
            End Set
        End Property

        Private _ChildTable As String
        Public Property ChildTable As String
            Get
                If _DataRelation IsNot Nothing Then
                    Return _DataRelation.ChildTable.TableName
                End If
                Return _ChildTable
            End Get
            Set(ByVal value As String)
                _ChildTable = value
                OnPropertyChanged("ChildTable")
                If _ChildTable Is Nothing Then
                    CType(_DetailsDataGrid.Columns(1), DataGridComboBoxColumn).ItemsSource = Nothing
                Else
                    CType(_DetailsDataGrid.Columns(1), DataGridComboBoxColumn).ItemsSource = _
                        ModuleDashboarding._activeproject.DataSet.Tables(_ChildTable).Columns.Cast(Of DataColumn).Select(Function(c) c.ColumnName).ToList()
                End If
            End Set
        End Property

    End Class

    Public Class DataRelationColumnWrapper
        Inherits UIObject

        Private _index As Integer
        Private _DataRelationWrapper As DataRelationWrapper


        Sub New()

        End Sub

        Sub New(dataRelationWrapper As DataRelationWrapper, index As Integer)
            ' TODO: Complete member initialization 
            _DataRelationWrapper = dataRelationWrapper
            _index = index
        End Sub

        Public ReadOnly Property IsEnabled As Boolean
            Get
                If _DataRelationWrapper Is Nothing Then Return True
                Return _DataRelationWrapper.DataRelation Is Nothing
            End Get
        End Property

        Private _ParentDataColumn As String
        Public Property ParentDataColumn As String
            Get
                If _DataRelationWrapper IsNot Nothing Then
                    If _DataRelationWrapper.DataRelation IsNot Nothing Then
                        Return _DataRelationWrapper.DataRelation.ParentColumns(_index).ColumnName
                    End If
                End If
                Return _ParentDataColumn
            End Get
            Set(ByVal value As String)
                _ParentDataColumn = value
                OnPropertyChanged("ParentDataColumn")
            End Set
        End Property

        Private _ChildDataColumn As String
        Public Property ChildDataColumn As String
            Get
                If _DataRelationWrapper IsNot Nothing Then
                    If _DataRelationWrapper.DataRelation IsNot Nothing Then
                        Return _DataRelationWrapper.DataRelation.ChildColumns(_index).ColumnName
                    End If
                End If
                Return _ChildDataColumn
            End Get
            Set(ByVal value As String)
                _ChildDataColumn = value
                OnPropertyChanged("ChildDataColumn")
            End Set
        End Property

        
    End Class
End Class

Public Class ViewModelDataStructure
    Inherits UIObject
    Implements IViewModelTabSection


    Sub New()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Title = "Structure"

        Icone = StandardIcons.GRID
    End Sub

    Public ReadOnly Property DataSet As DataSet
        Get
            Return ModuleDashboarding._activeproject.DataSet
        End Get
    End Property

    Public ReadOnly Property DataSourceList As DataSourceCollection
        Get
            Return ModuleDashboarding._activeproject.DataSourceList
        End Get
    End Property

    Public ReadOnly Property Zoom As Double
        Get
            Return Math.Pow(10, _LogZoom)
        End Get
    End Property

    Private _LogZoom As Double = 0
    Public Property LogZoom As Double
        Get
            Return _LogZoom
        End Get
        Set(ByVal value As Double)
            _LogZoom = value
            OnPropertyChanged("LogZoom")
            OnPropertyChanged("Zoom")
        End Set
    End Property



    Public Property Icone As String Implements IViewModelTabSection.Icone

    Public Property Title As String Implements IViewModelTabSection.Title

    Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable
End Class
